package Controller;

import java.util.ArrayList;
import java.util.Iterator;
import Model.*;

/*
 * Controller for DVD collection, singleton aswell since we dont want to create two different set of data.
 */
public class DVDCtr
{
    // fields
    // unique ID for each DVD, cannot by changed or given to other DVD.
    private int uniqueDvdID;
    // list of all DVDs
    private ArrayList<DVD> dvdList;
    private static DVDCtr instance = null;
    
    private DVDCtr()
    {
        // creating empty ArrayList to hold DVDs during the creation of object
        dvdList = new ArrayList<DVD>();
    }
    
    public static DVDCtr getInstance()       
               {
        if(instance == null)
            {
                instance = new DVDCtr();
            }
        return instance;
    }

    // creates a DVD and adds it to a list.
    public void createDVD(String title, String artist, String pubDate)
    {
        DVD dvd = new DVD(uniqueDvdID, title, artist, pubDate);
        dvdList.add(dvd);
        // increase the value of unique ID.
        uniqueDvdID++;
    }
    
    // finding a DVD specified by its uniqe ID.
    // creating a copy and adding it to the DVD.
    public void createCopy(int dvdId, String serialNumber, String purchaseDate, String purchasePrice)
    {
        Iterator<DVD> it = dvdList.iterator();
        boolean added = false;
        while(!added && it.hasNext()) {
            DVD dvd = it.next();
            if (dvd.getId() == dvdId) {
                dvd.addCopy(serialNumber, purchaseDate, purchasePrice);
                added = true;
            }
        }
    }
    
    // returns String representaion of information held in DVD, two DVD's can have same name.
    public String findDvd(String dvdTitle)
    {
        //Iterator<DVD> it = dvdList.iterator();
        String foundDvds = new String();
        for (DVD dvd : dvdList) {
            if (dvd.getTitle().equals(dvdTitle)) {
                foundDvds = foundDvds + dvd.toString();
            }
        }
        return foundDvds;
    }
    
    // returns String representation of information held in specified copy by serial number
    public String findCopyInfo(String serialNumber)
    {
        String foundCopy = new String();
        for(DVD dvd : dvdList)
        {
            foundCopy = foundCopy + dvd.findCopy(serialNumber);
        }
        return foundCopy;
    }
    
    // returns copy, by specified serial number
    public Copy findCopy(String serialNumber)
    {
        // new empty object of type copy.
        Copy copy = new Copy();
        // looking through all DVDs
        for(DVD dvd : dvdList)
        {
            // looking through all copies of each DVD
            for(Copy placeholder : dvd.getCopies()) {
                // comparing given serial number with serial numbers of copy
                if (placeholder.getSerialNumber().equals(serialNumber)){
                    copy = dvd.findCopy(serialNumber);
                }
            }
        }
        return copy;
    }
    
    // return String representation of informatin held by all of the DVDs
    public String listAllDvds()
    {
        String allInfo = new String();
        for(DVD dvd : dvdList)
        {
            allInfo = allInfo + dvd.toString();
        }
        return allInfo;
    }
    
    // finds and removes a DVD from a list
    public void deleteDvd(int dvdId)
    {
        Iterator<DVD> it = dvdList.iterator();
        boolean found = false;
        while(!found && it.hasNext()) {
            DVD dvd = it.next();
            if (dvd.getId() == dvdId) {
                dvdList.remove(dvd);
                found = true;
            }
        }
    }
    
    // finds and removes a Copy 
    public void deleteCopy(String serialNumber)
    {
        Iterator<DVD> it = dvdList.iterator();
        boolean deleted = false;
        while(!deleted && it.hasNext()) {
            DVD dvd = it.next();
            if (dvd.findCopy(serialNumber) != null) {
                dvd.removeCopy(serialNumber);
                deleted = true;
            }
        }
    }
}
